<?php
/**
 * web controller class
 *
 * @author raphael seebacher <raphasee@ee.ethz.ch>
 * @version 0.01
 */
class web_controller extends front_controller {
    protected $request = NULL;

    public static function is_valid_request_url($request) {
        return true;
    }

    public function process_request() {
        $request_string = rtrim($this->request->get_request_string(), '/');
        $user_controller = user_controller::get_instance();

        switch ($request_string) {
        case 'install':
            $this->view = new install_twimight_view();
            if ($user_controller->is_authenticated()) {
                $user = $user_controller->get_user();
                $this->view->set_user_information($user->get_all());
            }

            $this->view->send_header();
            echo $this->view->get_content();
            break;

        case 'logout':
            $session = session::get_instance();
            $session->clear();
            header('Location: /');
            break;

        case 'twitter/sign_in':
            if ($user_controller->is_authenticated()) {
                header('Location: /dashboard');
            }
            else {
                $redirect_url =
                    $user_controller->get_twitter_sign_in_redirect_url();
                header('Location: '.$redirect_url);
            }
            break;

        case 'twitter/callback':
            $user_controller->authenticate_from_twitter_callback($this->request);

            if ($user_controller->is_authenticated()) {
                header('Location: /dashboard');
            }
            else {
                header('Location: /');
            }
            break;

        case 'dashboard':
            if ($user_controller->is_authenticated()) {
                $user = $user_controller->get_user();

                $this->view = new dashboard_view();
                if ($user_controller->is_authenticated()) {
                    $user = $user_controller->get_user();
                    $this->view->set_user_information($user->get_all());
                }

                $this->view->send_header();
                echo $this->view->get_content();
            }
            else {
                header('Location: /twitter/sign_in');
            }
            break;

        case 'dashboard/user/delete':
            if ($user_controller->is_authenticated()) {
                $user = $user_controller->get_user();
                $user->delete();

                $session = session::get_instance();
                $session->clear();

                $this->view = new delete_view();
            }
            else {
                $this->view = new error_web_view(401);
            }

            $this->view->send_header();
            echo $this->view->get_content();
            break;

        case 'certificate/download':
            if ($user_controller->is_authenticated()) {
                $user = $user_controller->get_user();
                try {
                    $certificate = $user->get_current_certificate();
                    header(
                      'Content-disposition: attachment; filename=certificate_'
                      .$user->get_id().'_'.$user->get_screen_name().'.pem'
                    );
                    header('Content-type: application/x-pem-file');
                    echo $certificate->get('certificate');
                }
                catch (model_exception $e) {
                    $this->view = new error_web_view(404);
                    $this->view->send_header();
                    echo $this->view->get_content();
                }
            }
            else {
                $this->view = new error_web_view(401);
                $this->view->send_header();
                echo $this->view->get_content();
            }
            break;

        case 'crl':
            $crl_certificates = user_certificate::get_certificate_revocation_list();

            $crl = Array();
            foreach ($crl_certificates as $revoked_certificate) {
                $crl[] = Array(
                  'expiry' => $revoked_certificate->get_timestamp('valid_to'),
                  'revocation_time' => $revoked_certificate->get_timestamp('revoked'),
                  'serial_number' => $revoked_certificate->get('serial_number')
                );
            }

            $this->view = new json_view();
            $this->view->set_content($crl);
            $this->view->send_header();
            echo $this->view->get_content();

            break;

        case '':
            $this->view = new home_view();

            if ($user_controller->is_authenticated()) {
                $user = $user_controller->get_user();
                $this->view->set_user_information($user->get_all());
            }

            $this->view->send_header();
            echo $this->view->get_content();
            break;

        default:
            $this->view = new error_web_view(404);
            $this->view->send_header();
            echo $this->view->get_content();
        }
    }
}
?>